package com.jiyinit.web.core.activiti.service;

import com.jiyinit.web.core.activiti.dao.TabLeaveDealMindDao;
import com.jiyinit.web.core.activiti.entity.TabLeaveDealMindEntity;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author jack zh
 * @version 1.0
 * @date 2018/7/17
 * @since 1.0
 */

@Service("leaveDealMindServiceImpl")
public class LeaveDealMindServiceImpl {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(LeaveDealMindServiceImpl.class);

    @Resource(name = "core.tabLeaveDealMindDao")
    private TabLeaveDealMindDao tabLeaveDealMindDao;


    //工作流相关
    @Resource(name = "runtimeService")
    RuntimeService runtimeservice;
    @Resource(name = "identityService")
    IdentityService identityservice;
    @Resource(name = "taskService")
    TaskService taskservice;


    public int deleteByPrimaryKey(Integer id)
    {
        return tabLeaveDealMindDao.deleteByPrimaryKey(id);
    }


    public int insert(TabLeaveDealMindEntity record)
    {
        return tabLeaveDealMindDao.insert(record);
    }


    public int insertSelective(TabLeaveDealMindEntity record)
    {
        return tabLeaveDealMindDao.insertSelective(record);
    }

    public TabLeaveDealMindEntity selectByPrimaryKey(Integer id)
    {
        return tabLeaveDealMindDao.selectByPrimaryKey(id);
    }

    public List<TabLeaveDealMindEntity> findBySelective(TabLeaveDealMindEntity record)
    {
        return tabLeaveDealMindDao.findBySelective(record);
    }

    public int updateByPrimaryKeySelective(TabLeaveDealMindEntity record)
    {
        return tabLeaveDealMindDao.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(TabLeaveDealMindEntity record)
    {
        return tabLeaveDealMindDao.updateByPrimaryKey(record);
    }

    /**
     * 查看当前人之前处理过的节点 若处理了 就直接完成
     * @param delegateTask
     * @param mainUserId  主要负责人
     */
    public void defalutDealTask(DelegateTask delegateTask,String mainUserId)
    {
        String extId=delegateTask.getExecutionId();
        //查询当前处理人 有没有之前处理过
        TabLeaveDealMindEntity record=new TabLeaveDealMindEntity();
        record.setExecutionId(extId);
        record.setStatus("1");
        record.setDealUserid(Integer.parseInt(mainUserId)); //处理人id
        List<TabLeaveDealMindEntity>  dealList=this.findBySelective(record);
        if(dealList.size()>0)  //以前处理过了 直接越过
        {
            taskservice.claim(delegateTask.getId(),mainUserId);  //可能存在多个人 必须先拾取
            taskservice.complete(delegateTask.getId());   //直接完成
            // 保存节点处理意见
            record.setCreateDate(new Date());
            record.setTaskId(delegateTask.getId());
            record.setTaskName(delegateTask.getName());
            record.setDealMindInfo("默认通过");
            record.setIsAgree("1");  //同意
            this.insertSelective(record);
        }

    }

    

}



